home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr34 / onetime.zip / CVT.C next >
C/C++ Source or Header  |  1995-04-26  |  3KB  |  171 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4.  
  5. #define NUMBERS 10 
  6. #define INVOCATION_ERROR 20
  7. #define FILE_ERR 11
  8. #define BUFFERSIZE 8192
  9. #define FILENAME argv[1]
  10. #define TRUE 1
  11. #define FALSE 0
  12. #define NUMCVT 48
  13. #define PARAM_NO 2
  14. #define APOSTROPHE 39
  15. #define DOLLAR_SIGN '$'
  16.  
  17.  
  18. const char *Table [NUMBERS] = {
  19.                   "ZERO",
  20.                   "ONE",
  21.                   "TWO",
  22.                   "THREE",
  23.                   "FOUR",
  24.                   "FIVE",
  25.                   "SIX",
  26.                   "SEVEN",
  27.                   "EIGHT",
  28.                   "NINE"
  29.                   };
  30.  
  31.  
  32. long code( char *filename );
  33.  
  34.  
  35.  
  36.  
  37.  
  38. void main( int argc, char **argv )
  39. {
  40.    long no;
  41.  
  42.      if( argc != PARAM_NO )
  43.         {
  44.         printf( "\nForm: cvt FILENAME." );
  45.         exit( INVOCATION_ERROR );
  46.         }
  47.  
  48.      no = code( FILENAME );
  49.  
  50.      printf( "\n%ld characters converted in the file %s.", no, FILENAME );
  51.  
  52.  
  53. }
  54.  
  55.  
  56.  
  57.  
  58. long code( char *filename )
  59. {
  60.  
  61.    register int c;
  62.    long number = 0;
  63.  
  64.    FILE *plaintxt,
  65.        *cvtfile;
  66.  
  67.  
  68.       if( NULL == ( plaintxt = fopen( filename, "r" ) ) )
  69.          exit( FILE_ERR );
  70.       if( setvbuf( plaintxt, NULL, _IOFBF, BUFFERSIZE ) )
  71.          exit( FILE_ERR );
  72.  
  73.       if( NULL == ( cvtfile = fopen( "file.cvt", "w" ) ) )
  74.          exit ( FILE_ERR );
  75.       if( setvbuf( cvtfile, NULL, _IOFBF, BUFFERSIZE ) )
  76.          exit( FILE_ERR );
  77.  
  78.  
  79.    while(  EOF != ( c = fgetc( plaintxt ) ) )
  80.      {
  81.       if( isdigit( c ) )
  82.         {
  83.         c -= NUMCVT;
  84.         fputs( Table [c], cvtfile );
  85.         }
  86.  
  87.       else
  88.         if( c == '$' )
  89.           fputs( "DOLLARSIGN", cvtfile );
  90.  
  91.       else
  92.         if( ispunct( c ) )
  93.            switch( c )
  94.              {
  95.              case '.':
  96.                 fputs( "PERIOD", cvtfile );
  97.                 break;
  98.              case ',':
  99.                 fputs( "COMMA", cvtfile );
  100.                 break;
  101.              case ':':
  102.                 fputs( "COLON", cvtfile );
  103.                 break;
  104.              case ';':
  105.                 fputs( "SEMICOLON", cvtfile );
  106.                 break;
  107.              case '"':
  108.                 fputs( "QUOTE", cvtfile );
  109.                 break;
  110.              case '!':
  111.                 fputs( "EXCLAMATIONPOINT", cvtfile );
  112.                 break;
  113.              case APOSTROPHE:
  114.                 fputs( "APOSTROPHE", cvtfile );
  115.                 break;
  116.     case '/':
  117.        fputs( "SLASH", cvtfile );
  118.        break;
  119.     case '\\':
  120.        fputs( "BACKSLASH", cvtfile );
  121.        break;
  122.     case '(':
  123.        fputs( "LEFTPAREN", cvtfile );
  124.        break;
  125.     case ')':
  126.        fputs( "RIGHTPAREN", cvtfile );
  127.        break;
  128.     case '*':
  129.        fputs( "ASTERISK", cvtfile );
  130.        break;
  131.     case '-':
  132.        fputs( "DASH", cvtfile );
  133.        break;
  134.     case '&':
  135.        fputs( "AMPERSAND", cvtfile );
  136.        break;
  137.     case '$':
  138.        fputs( "DOLLARSIGN", cvtfile );
  139.        break;
  140.     case '%':
  141.        fputs ( "PERCENT", cvtfile );
  142.        break;
  143.     case '#':
  144.        fputs ( "POUNDSIGN", cvtfile );
  145.        break;
  146.     case '?':
  147.        fputs( "QUESTIONMARK", cvtfile );
  148.        break;
  149.     case '=':
  150.        fputs ( "EQUALS", cvtfile );
  151.        break;
  152.     case '+':
  153.        fputs( "PLUS", cvtfile );
  154.        break;
  155.              }
  156.  
  157.       else
  158.         fputc( c, cvtfile );
  159.  
  160.       number++;
  161.  
  162.        }
  163.  
  164.    fcloseall();
  165.  
  166.    return( number );
  167.  
  168. }
  169.  
  170.  
  171.